<?php

namespace app\api\controller;

class Log extends Base
{
    /**
     * 获取列表
     */
    public function list($start_time = 0, $end_time = 9999999999, $match = "", $limit = 10, $page = 1)
    {
        $map = [
            'log.create_time' => ['between', [$start_time ? $start_time : 0, $end_time ? $end_time : 9999999999]],
            //模糊匹配
            'user.username|log.ip' => ['like', '%' . trim($match) . '%']
        ];
        $result = Model('Log')->getDataList($map, $limit, $page);
        if (!$result) {
            return resultArray(['error' => Model('Log')->getError()]);
        }
        return resultArray(['data' => $result]);
    }


    /**
     * 获取log详情
     */
    public function detail($id)
    {
        $result = Model('Log')->getDetail($id);
        if (!$result) {
            return resultArray(['error' => Model('Log')->getError()]);
        }
        return resultArray(['data' => $result]);
    }

    /**
     * 导出日志
     */
    public function download($id = 0, $start_time = 0, $end_time = 9999999999){
        $map['create_time'] = ['between', [$start_time ? $start_time : 0, $end_time ? $end_time : 9999999999]];
        if($id){
            $map['uid'] = $id;
        }
        $result = Model('Log')->getAllData($map);
        if (!$result) {
            return resultArray(['error' => Model('Log')->getError()]);
        }
        download_result_one($result);
    }
}
